package cn.itheima.web;

import cn.itheima.dao.UserDao;
import cn.itheima.domain.User;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: cuisensen
 * data: 2018/6/28.
 * time: 21:09.
 */
@WebServlet("/CheckUsernameServlet")
public class CheckUsernameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = null != request.getParameter("flag") ? request.getParameter("flag") : "";
        String json=null;
        if(!s.equals("exist")) {
            String uname = null != request.getParameter("username") ? request.getParameter("username") : "";
            UserService us = new UserServiceImp();
            boolean isExist = us.isExist(uname);
            json = "{\"isExist\":" + isExist + "}";
        }else {
            json = "{\"isExist\":" + false + "}";
        }
        response.getWriter().write(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
